using System.Collections;
using Microsoft.Xna.Framework.Input;

namespace FITClub.Episode1.Controller
{
    public struct VirtualGamepadState
    {
        public static VirtualGamepadState Default { get { return new VirtualGamepadState(new byte[] { 0 }); } }

        BitArray states;

        public VirtualGamepadState(byte[] state)
        {
            this.states = new BitArray(state);
        }

        public byte ConvertToByte()
        {
            byte[] ba = new byte[1];
            this.states.CopyTo(ba, 0);
            return ba[0];
        }

        public VirtualGamepadState Copy()
        {
            VirtualGamepadState vgps = Default;
            BitArray ba =  vgps.states.Or(this.states);
            vgps.states = ba;
            return vgps;
        }


        public bool this[int index]
        {
            get { return states[index]; }
            set { states[index] = value; }
        }

        public ButtonState Up
        {
            get { return this.GetButtonSate(0); }
        }

        public ButtonState Left
        {
            get { return this.GetButtonSate(1); }
        }

        public ButtonState Down
        {
            get { return this.GetButtonSate(2); }
        }

        public ButtonState Right
        {
            get { return this.GetButtonSate(3); }
        }

        public ButtonState Attack
        {
            get { return this.GetButtonSate(4); }
        }

        public ButtonState Defence
        {
            get { return this.GetButtonSate(5); }
        }

        public ButtonState GetButtonSate(int button)
        {
            return (this.states[button]) ? ButtonState.Pressed : ButtonState.Released;
        }
    }
}